package com.taotao.job.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import com.taotao.job.dao.JobLockMapper;
import com.taotao.job.model.entity.JobLock;
import com.taotao.job.service.JobLockService;
import com.taotao.job.util.EntityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Set;

/**
 * <p>
 * 锁服务接口实现类
 * </p>
 *
 * @author Fan Beibei
 * @version V1.0
 * @since 2021-03-14 21:43:18
 */
@Slf4j
@Service
public class JobLockServiceImpl implements JobLockService {
    @Resource
    private JobLockMapper jobLockMapper;


    @Override
    public void softDelByLockNames (Set<java.lang.String> lockNames, Long userId) {
        Preconditions.checkArgument(null != lockNames && lockNames.size() > 0);
        Preconditions.checkArgument(null != userId);

        jobLockMapper.softDelByPks(lockNames,userId,new Date());
    }

    @Override
    public JobLock queryByLockName (java.lang.String lockName) {
        Preconditions.checkArgument(null != lockName);

        return jobLockMapper.selectByPk(lockName);
    }

    @Override
    public void addOrUpdate(JobLock jobLock, Long userId) {
        Preconditions.checkArgument(null != jobLock);
        Preconditions.checkArgument(null != userId);

        if (null == jobLock.getLockName()) {
            EntityUtils.setPo5Field(userId, new Date(), jobLock);
            jobLockMapper.insert(jobLock);
        } else {
            EntityUtils.setPo2Field(userId, new Date(), jobLock);
            //这里不做删除
            jobLockMapper.updateByPk(jobLock);
        }

    }

    @Override
    public PageInfo<JobLock> queryByPage(JobLock param, int pageNum, int pageSize) {
        Preconditions.checkArgument(pageNum > 0);
        Preconditions.checkArgument(pageSize > 0);

        Page<JobLock> page =  jobLockMapper.selectByPage(param,pageNum,pageSize);

        return new PageInfo<>(page);
    }






//------------以上是生成代码-----------------------------------------------------------------------------------------------------------------









}
